package com.lms.service.impl;

import com.lms.bean.Book;
import com.lms.bean.RentRecord;
import com.lms.bean.ReturnRecord;
import com.lms.bean.User;
import com.lms.dao.BookDao;
import com.lms.dao.RentRecordDao;
import com.lms.dao.ReturnRecordDao;
import com.lms.dao.UserDao;
import com.lms.service.ReturnRecordService;
import com.lms.vo.RentRecordVO;
import com.lms.vo.ReturnRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ReturnRecordServiceImpl implements ReturnRecordService {

    @Autowired
    private ReturnRecordDao returnRecordDao;

    @Autowired
    private RentRecordDao rentRecordDao;

    @Autowired
    private BookDao bookDao;

    @Autowired
    private UserDao userDao;

    @Override
    public Long getTotalCount(String username, String bookname) {
        return returnRecordDao.countByBookNameAndUsername(username,bookname);
    }

    @Override
    public Long getTotalReturnsCount(Long userId, String bookname) {
        return rentRecordDao.countByUserIdAndAndBookName(userId, bookname);
    }

    @Override
    public List<ReturnRecordVO> findAll(int page, int pageSize, String username, String bookname) {
        Sort sort = Sort.by(Sort.Direction.ASC, "returnId");
        Pageable pageable = PageRequest.of(page, pageSize, sort);
        List<ReturnRecord> list = returnRecordDao.getList(username,bookname,pageable);
        List<ReturnRecordVO> returnRecordVOList = new ArrayList<>();
        list.forEach(returnRecord -> returnRecordVOList.add(ReturnRecordVO.convert(returnRecord)));
        return returnRecordVOList;
    }

    @Override
    public List<RentRecordVO> findAllReturns(int page, int pageSize, User user, String bookname) {
        Sort sort = Sort.by(Sort.Direction.ASC, "rentId");
        Pageable pageable = PageRequest.of(page, pageSize, sort);
        List<RentRecord> list = returnRecordDao.getReturnsList(user.getUserId(), bookname, pageable);
        List<RentRecordVO> rentRecordVOList = new ArrayList<>();
        list.forEach(rentRecord -> rentRecordVOList.add(RentRecordVO.convert(rentRecord)));
        return rentRecordVOList;
    }

    @Override
    public void deleteById(Long returnId) {
         returnRecordDao.deleteById(returnId);
    }

    @Override
    public ReturnRecord findById(Long returnId) {
        return returnRecordDao.getOne(returnId);
    }

    @Override
    public void save(ReturnRecord returnRecord) {
        Long bookId = returnRecord.getBook().getBookId();
        Book one = bookDao.getOne(bookId);
        returnRecord.setBook(one);
        Long userId = returnRecord.getUser().getUserId();
        User one1 = userDao.getOne(userId);
        returnRecord.setUser(one1);
        if (returnRecord.getRentTime()==null){
            returnRecord.setRentTime(this.returnRecordDao.getOne(returnRecord.getReturnId()).getRentTime());
        }
        returnRecordDao.save(returnRecord);
    }

    /**
     * 添加还书订单后需要使得书籍在馆数量+1
     * @param returnRecord
     */
    @Override
    public void returnBook(ReturnRecord returnRecord) {
        Long bookId = returnRecord.getBook().getBookId();
        Book book = bookDao.getOne(bookId);
        returnRecord.setBook(book);
        Long userId = returnRecord.getUser().getUserId();
        User user = userDao.getOne(userId);
        returnRecord.setUser(user);
        returnRecordDao.save(returnRecord);
        book.setBookNumber(book.getBookNumber()+1);
        bookDao.save(book);
    }

    @Override
    public void deleteAll(List<Long> ids) {
        returnRecordDao.deleteAll(ids);
    }

}
